package com.hurl.library.util;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class StringUtils {
	public static String concat(String[] array, String c){
		if(array==null || array.length==0){
			return null;
		}
		String res = array[0];
		for(int i=1;i<array.length;i++){
			res+=c+array[i];
		}
		return res;
	}
	public static String splitList(List<String> list){
		if(list==null || list.isEmpty()){
			return "";
		}
		String res = null;
		boolean first = true;
		for(int i=0;i<list.size();i++){
			if(StringUtils.isBlank(list.get(i))){
				continue;
			}
			if(first){
				res=list.get(i);
				first=false;
			}else{
				res+=","+list.get(i);
			}
		}
		return res;
	}
	public static final String fillPlaces(int i, int place) {
		if (i < 0) {
			return String.valueOf(i);
		}
		String str = String.valueOf(i);
		while (str.length() < place) {
			str = "0" + str;
		}
		return str;
	}

	public static String getClassName(Object obj) {
		return obj == null ? "null" : obj.getClass().getName();
	}

	public static List<String> substringsBetween(String str, String open, String close) {
		if (isEmpty(str)) {
			return null;
		}

		int start = 0;
		if (isNotEmpty(open)) {
			start = str.indexOf(open);
			if (start == -1) {
				return null;
			}
			start = start + open.length();
		}

		int end = str.length();
		if (isNotEmpty(close)) {
			end = str.indexOf(close, start);
			if (end == -1) {
				return null;
			}
		}
		
		List<String> list = new ArrayList<String>();
		String substr = str.substring(start, end);
		list.add(substr);
		int nextstart = end+(isNotEmpty(close)?close.length():0);
		if(nextstart<str.length()){
			List<String> sublist = substringsBetween(str.substring(nextstart),open,close);
			if(sublist!=null){
				list.addAll(sublist);
			}
		}
		return list;
		
	}

	public static String substringBetween(String str, String open, String close) {
		if (isEmpty(str)) {
			return null;
		}

		int start = 0;
		if (isNotEmpty(open)) {
			start = str.indexOf(open);
			if (start == -1) {
				return null;
			}
			start = start + open.length();
		}

		int end = str.length();
		if (isNotEmpty(close)) {
			end = str.indexOf(close, start);
			if (end == -1) {
				return null;
			}
		}

		return str.substring(start, end);
	}

	/*
	 * public static String[] substringsBetween(String str, String open, String
	 * close) { if(str == null || isEmpty(open) || isEmpty(close)) return null;
	 * int strLen = str.length(); if(strLen == 0) return
	 * ArrayUtils.EMPTY_STRING_ARRAY; int closeLen = close.length(); int openLen
	 * = open.length(); List list = new ArrayList(); int end; for(int pos = 0;
	 * pos < strLen - closeLen; pos = end + closeLen) { int start =
	 * str.indexOf(open, pos); if(start < 0) break; start += openLen; end =
	 * str.indexOf(close, start); if(end < 0) break;
	 * list.add(str.substring(start, end)); }
	 * 
	 * if(list.size() > 0) return (String[])list.toArray(new
	 * String[list.size()]); else return null; }
	 */

	public static String changeCharset(String text, String oldCharset,
			String newCharset) {
		if (isBlank(text)) {
			return text;
		}

		try {
			byte[] bs = text.getBytes(oldCharset);
			return new String(bs, newCharset);
		} catch (UnsupportedEncodingException e) {
			return text;
		}
	}

	public static boolean isBlank(String str) {
		return str == null || str.trim().length()==0;
	}

	public static boolean isNotBlank(String str) {
		return !isBlank(str);
	}
	
	public static boolean isEmpty(String str) {
		return str == null || str.length()==0;
	}

	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}
}
